package com.actions;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.struts2.ServletActionContext;

import com.bean.SupplierProduct;
import com.opensymphony.xwork2.ActionSupport;
import com.serviceImpl.SupplierProductServiceImpl;
import com.serviceImpl.UtilSupport;


public class SupplierProductAction extends ActionSupport {

	//封装
	private SupplierProductServiceImpl supplierProductBiz;	//供应商service
	private UtilSupport util;		//公用类
	
	//集合
	private List<SupplierProduct> supplierProductList;	//供应商询价列表

	//分页显示
	private int offset;			//当前页
	private int pageSize=10;
	private int totalcount;		// 总记录数
	private int totalpage; 		// 总页数
	
	//查询条件
	private String productCode;//款号
	private String priceSt;//开始报价
	private String priceEd;//结束报价
	private String createtimeSt;//开始时间
	private String createtimeEd;//结束时间

	
	//json格式
	private Boolean flag;	//修改成功返回true、false
	
	//get、set
	public SupplierProductServiceImpl getSupplierProductBiz() {
		return supplierProductBiz;
	}

	public void setSupplierProductBiz(SupplierProductServiceImpl supplierProductBiz) {
		this.supplierProductBiz = supplierProductBiz;
	}

	public List<SupplierProduct> getSupplierProductList() {
		return supplierProductList;
	}

	public void setSupplierProductList(List<SupplierProduct> supplierProductList) {
		this.supplierProductList = supplierProductList;
	}
	
	public UtilSupport getUtil() {
		return util;
	}

	public void setUtil(UtilSupport util) {
		this.util = util;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	
	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public String getPriceSt() {
		return priceSt;
	}

	public void setPriceSt(String priceSt) {
		this.priceSt = priceSt;
	}

	public String getPriceEd() {
		return priceEd;
	}

	public void setPriceEd(String priceEd) {
		this.priceEd = priceEd;
	}

	public String getCreatetimeSt() {
		return createtimeSt;
	}

	public void setCreatetimeSt(String createtimeSt) {
		this.createtimeSt = createtimeSt;
	}

	public String getCreatetimeEd() {
		return createtimeEd;
	}

	public void setCreatetimeEd(String createtimeEd) {
		this.createtimeEd = createtimeEd;
	}

	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	/**
	 * 获取所有供应商报价
	 * @return
	 */
	public String getAllSuplierProduct(){
		HttpServletRequest request = ServletActionContext.getRequest();
		try {

			StringBuffer sqlbf=new StringBuffer("select * from audit.supplier_product WHERE 0=0");
			String codeSql = "";
			String priceSql ="";
			String timeSql = "";
			
			//产品款号
			this.productCode = request.getParameter("productCode");
			if(productCode != null&&!productCode.isEmpty()){
				productCode=new String(productCode.trim().getBytes("ISO-8859-1"),"UTF-8");
				codeSql=" and product_code like '%"+productCode+"%'";
				sqlbf.append(codeSql);
			}
			
			//产品报价
			this.priceEd = request.getParameter("priceSt");//开始数量
			this.priceEd = request.getParameter("priceEd");//结束数量
			
			if(priceEd!=null&&!priceEd.isEmpty()&&priceEd!=null&&!priceEd.isEmpty()){
				Double sSt=Double.parseDouble(priceEd);
				Double sEd=Double.parseDouble(priceEd);
				
				if (sSt<sEd) {
					priceSql="and( price  between "+sSt+" and "+sEd+")";
				}else {
					priceSql="and( price  between "+sEd+" and "+sSt+")";
				}
				
				sqlbf.append(priceSql);
			}
			
			//创建时间
			this.createtimeSt = request.getParameter("createtimeSt");//开始数量
			this.createtimeEd = request.getParameter("createtimeEd");//结束数量
			
			if(createtimeSt!=null&&!createtimeSt.isEmpty()&&createtimeEd!=null&&!createtimeEd.isEmpty()){
				//格式化时间
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
				
				Date sDate = sim.parse(createtimeSt);
				Timestamp s = new Timestamp(sDate.getTime());
				
				Date eDate = sim.parse(createtimeEd);
				Timestamp e = Timestamp.valueOf(DateFormatUtils.format(eDate, "yyyy-MM-dd")+" 23:59:59");;
				
				
				timeSql="and (create_time  between '"+s+"' and '"+e+"')";
				
				sqlbf.append(timeSql);
				
			}
			
			
			
			String sql =sqlbf.toString();
			
			totalcount = util.getTotalCount(sql);		//获取总条数

			totalpage = totalcount % pageSize == 0 ? totalcount / pageSize 
					: totalcount / pageSize + 1;		//获取总页数

			offset = getPageOffset();
			
			//表名
			String table="v_supplier_product";
			//主键
			String pk="sup_id";
			//排序字段
			String sort="create_time";
			//当前页数
			int PageNumber=offset+1;
			
			int PageSize=pageSize;

			//查询条件
			
			String options=(codeSql+priceSql+timeSql).equals("")?"":("0=0"+codeSql+priceSql+timeSql);
			supplierProductList=supplierProductBiz.getAllSupplierProductList(table, pk, sort, PageNumber, PageSize, options);
		
		} catch (Exception e) {
			
		}
		return "supList";
	}
	
	// Added by JSL : 获取翻页偏移量(实际上是将要翻到的页面的页索引，页索引从 0 开始)
		private int getPageOffset() {
			HttpServletRequest request=ServletActionContext.getRequest();
			String ofst = request.getParameter("offset");
			int idx = 0;
			if(ofst!=null){
				idx = Integer.valueOf(ofst);
				idx = idx < 0 ? 0 : idx;                        // 超过第一页时，不再翻页
				idx = idx >= totalpage ? (totalpage-1) : idx;	// 超过最后一页时，不再翻页		
			}
			return idx;
		}


	/**
	 * 中标供应商报价
	 */
	public String selectedSuna(){
		HttpServletRequest request=ServletActionContext.getRequest();
		String id = request.getParameter("supId");
		Integer supId=Integer.parseInt(id);
		
		//修改供应商报价表的状态和采购需求表的状态
		
		flag=supplierProductBiz.getSelectedSuna(supId);
		
		return "select";
		
	}

	
}
